#include <QtWidgets/QApplication>
#include <QtCore/QDir>
#include <QtCore/QStandardPaths>
#include <QtWidgets/QMessageBox>
#include <QtCore/QTextCodec>
#include <QtCore/QTranslator>
#include <QtCore/QLibraryInfo>
#include "ScannerUI.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    
    // 设置应用程序信息
    app.setApplicationName("FileScannerUI");
    app.setApplicationVersion("1.0");
    app.setOrganizationName("YiTu Vision");
    app.setOrganizationDomain("yitu.com");
    
    // 设置编码
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
#endif
    
    // 设置中文字体
    QFont font("Microsoft YaHei", 9);
    app.setFont(font);
    
    // 加载中文翻译
    QTranslator translator;
    const QStringList uiLanguages = QLocale::system().uiLanguages();
    for (const QString &locale : uiLanguages) {
        const QString baseName = "qt_" + QLocale(locale).name();
        if (translator.load(":/i18n/" + baseName)) {
            app.installTranslator(&translator);
            break;
        }
    }
    
    // 设置样式表
    QString styleSheet = R"(
        QMainWindow {
            background-color: #f5f5f5;
        }
        
        QGroupBox {
            font-weight: bold;
            border: 2px solid #cccccc;
            border-radius: 5px;
            margin-top: 1ex;
            padding-top: 10px;
        }
        
        QGroupBox::title {
            subcontrol-origin: margin;
            left: 10px;
            padding: 0 5px 0 5px;
        }
        
        QPushButton {
            background-color: #ffffff;
            border: 1px solid #cccccc;
            border-radius: 4px;
            padding: 6px 12px;
            font-size: 14px;
        }
        
        QPushButton:hover {
            background-color: #e6e6e6;
            border-color: #adadad;
        }
        
        QPushButton:pressed {
            background-color: #d4edda;
            border-color: #c3e6cb;
        }
        
        QPushButton:disabled {
            background-color: #f8f9fa;
            color: #6c757d;
            border-color: #dee2e6;
        }
        
        QLineEdit {
            border: 1px solid #cccccc;
            border-radius: 4px;
            padding: 6px;
            font-size: 14px;
        }
        
        QLineEdit:focus {
            border-color: #007bff;
            outline: none;
        }
        
        QListWidget {
            border: 1px solid #cccccc;
            border-radius: 4px;
            background-color: #ffffff;
            alternate-background-color: #f8f9fa;
        }
        
        QListWidget::item {
            padding: 4px;
            border-bottom: 1px solid #eeeeee;
        }
        
        QListWidget::item:selected {
            background-color: #007bff;
            color: white;
        }
        
        QListWidget::item:hover {
            background-color: #e3f2fd;
        }
        
        QTextEdit {
            border: 1px solid #cccccc;
            border-radius: 4px;
            background-color: #ffffff;
            font-family: "Consolas", "Monaco", monospace;
        }
        
        QProgressBar {
            border: 1px solid #cccccc;
            border-radius: 4px;
            text-align: center;
        }
        
        QProgressBar::chunk {
            background-color: #007bff;
            border-radius: 3px;
        }
        
        QStatusBar {
            background-color: #f8f9fa;
            border-top: 1px solid #dee2e6;
        }
        
        QMenuBar {
            background-color: #ffffff;
            border-bottom: 1px solid #dee2e6;
        }
        
        QMenuBar::item {
            padding: 4px 8px;
        }
        
        QMenuBar::item:selected {
            background-color: #e9ecef;
        }
    )";
    
    app.setStyleSheet(styleSheet);
    
    try {
        // 创建主窗口
        ScannerUI window;
        window.show();
        
        return app.exec();
    }
    catch (const std::exception& e) {
        QMessageBox::critical(nullptr, "致命错误", 
            QString("应用程序启动失败: %1").arg(e.what()));
        return -1;
    }
    catch (...) {
        QMessageBox::critical(nullptr, "致命错误", "应用程序启动失败: 未知错误");
        return -1;
    }
}